#!/bin/sh -
#===============================================================================
#
#          FILE: system-generator-check-gpu
#
#   DESCRIPTION: The goal are to detect if the gpu are present/loaded or not
#  ORGANIZATION: STMicroelectronics
#     COPYRIGHT: Copyright (C) 2018, STMicroelectronics - All Rights Reserved
#===============================================================================

check_dt_status() {
    gcnano_path=$(find /proc/device-tree/soc* | grep gpu | head -n 1)
    gcnano_dir=$(basename $gcnano_path)
    if [ -z "$gcnano_dir" ] || [ ! -d $gcnano_path ];
    then
        echo "Gcnano in /proc/device-tree/soc/ is not available" > /dev/kmsg
        force_no_dtb_gpu="1"
    else
        if [ ! -f $gcnano_path/status ]; then
            gcnano_status="okay"
        else
            if $(grep -q "okay" $gcnano_path/status); then
                gcnano_status="okay"
            fi
        fi
    fi
}

check_load_module() {
    local _timeout=0
    if [ -n "$(cat /proc/modules | grep etnaviv)"  ]; then
        galcore_module=$(cat /proc/modules | grep ^etnaviv)
        return
    fi
    while [ -z "$(cat /proc/modules | grep galcore)" ]; do
        sleep 1
        _timeout=$(($_timeout+1))
        if [ "$_timeout" -eq "10" ]; then
            echo "Time out check load galcore module expired" > /dev/kmsg
            break
        fi
    done
    galcore_module=$(cat /proc/modules | grep galcore)
}

check_devices() {
    local _timeout=0
    if [ -n "$(cat /proc/modules | grep etnaviv)"  ]; then
        galcore_device=$(cat /proc/modules | grep ^etnaviv)
        return
    fi
    while [ -z "$(ls -l /dev/ | grep galcore)" ]; do
        sleep 1
        _timeout=$(($_timeout+1))
        if [ "$_timeout" -eq "10" ]; then
            echo "Time out check galcore device expired" > /dev/kmsg
            break
        fi
    done
    galcore_device=$(ls -l /dev/ | grep galcore)
}

check_default_weston_config() {
    if [ "$force_no_dtb_gpu" = "1" ];
    then
        if [ -e /etc/default/weston ] && $(grep -q "Autogenerated" /etc/default/weston);
        then
            default_weston_status="Autogenerated"
        fi
    fi
}
#
# Main
#
gcnano_status="disabled"
galcore_module=""
galcore_device=""
default_weston_status=""
force_no_dtb_gpu=0

check_dt_status
check_default_weston_config
if [ "$default_weston_status" = "Autogenerated" ];
then
    echo "Weston already configured"
    exit 0
fi

check_load_module
check_devices

if [ "$gcnano_status" = "okay" ] && [ -n "$galcore_module" ] && [ -n "$galcore_device" ];
then
    #echo "Gcnano is present and activated" > /dev/kmsg
    if [ -f /etc/default/weston ] && $(grep -q "Autogenerated" /etc/default/weston) ;
    then
        sed -i "s/OPTARGS=--use-pixman/#OPTARGS=--use-pixman/g" /etc/default/weston
        sed -i "/#Autogenerated/d" /etc/default/weston
    fi
    if [ ! -f /etc/default/weston ];
    then
        echo "#Autogenerated" > /etc/default/weston
        echo "#OPTARGS=--use-pixman" >> /etc/default/weston
        echo "WESTON_USER=root" >> /etc/default/weston
    fi
else
    if [ -f /etc/default/weston ] && $(grep -q "pixman" /etc/default/weston) ;
    then
        echo "Weston already configured on pixman" > /dev/kmsg
    else
        echo "Configure weston on pixman" > /dev/kmsg
        echo "#Autogenerated" > /etc/default/weston
        echo "OPTARGS=--use-pixman" >> /etc/default/weston
        echo "WESTON_USER=root" >> /etc/default/weston
    fi
fi

dri_device=$(ls -l /dev/ | grep dri)
if [ -z "$dri_device" ];
then
    exit 1
fi
